package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.inner.GroupTreeNode;
import com.augurit.common.base.extmodel.inner.OrgTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmPosition;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;

public class OmPositionConverter {

	public static void convertToEntity(OmPositionForm form, OmPosition entity){
		if(form != null && entity != null){

			if(form.getPosId() != null)
				entity.setPosId(form.getPosId());
			
			if(form.getPosCode() != null && form.getPosCode().trim().length() > 0)
				entity.setPosCode(form.getPosCode());
			
			if(form.getPosName() != null && form.getPosName().trim().length() > 0)
				entity.setPosName(form.getPosName());
			
			if(form.getPosLevel() != null)
				entity.setPosLevel(form.getPosLevel());			
			
			if(form.getParentPosId() != null)
				entity.setParentPosId(form.getParentPosId());
			
			if(form.getPartyId() != null)
				entity.setPartyId(form.getPartyId());
			
			if(form.getSortNo() != null)
				entity.setSortNo(form.getSortNo());
			
			if(form.getPosType() != null && form.getPosType().trim().length() > 0)
				entity.setPosType(form.getPosType());
			
			if(form.getCreator() != null && form.getCreator().trim().length() > 0)
				entity.setCreator(form.getCreator());
			
			if(form.getCreateTime() != null)
				entity.setCreateTime(form.getCreateTime());
			
			if(form.getUpdator() != null && form.getUpdator().trim().length() > 0)
				entity.setUpdator(form.getUpdator());
			
			if(form.getUpdateTime() != null)
				entity.setUpdateTime(form.getUpdateTime());
			
			if(form.getStatus() != null && form.getStatus().trim().length() > 0)
				entity.setStatus(form.getStatus());
			
			if(form.getIsLeaf() != null && form.getIsLeaf().trim().length() > 0)
				entity.setIsLeaf(form.getIsLeaf());
			
			if(form.getSubCount() != null)
				entity.setSubCount(form.getSubCount());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
			
			if(form.getPosSeq() != null && form.getPosSeq().trim().length() > 0)
				entity.setPosSeq(form.getPosSeq());
		}
	}
	
	public static OmPositionForm convertToForm(OmPosition entity){
		if(entity != null){
			OmPositionForm form = new OmPositionForm();
			
			form.setPosId(entity.getPosId());
			form.setPosCode(entity.getPosCode());
			form.setPosName(entity.getPosName());
			form.setPosLevel(entity.getPosLevel());
			form.setParentPosId(entity.getParentPosId());
			form.setPartyId(entity.getPartyId());
			form.setSortNo(entity.getSortNo());
			form.setPosType(entity.getPosType());
			form.setCreator(entity.getCreator());
			form.setCreateTime(entity.getCreateTime());
			form.setUpdator(entity.getUpdator());
			form.setUpdateTime(entity.getUpdateTime());
			form.setStatus(entity.getStatus());
			form.setIsLeaf(entity.getIsLeaf());
			form.setSubCount(entity.getSubCount());
			form.setRemark(entity.getRemark());
			form.setPosSeq(entity.getPosSeq());
			
			return form;
		}else
			return null;
	}
	
	/**
	 * 将岗位Form对象列表转换为OrgTreeNode对象列表
	 * @param result
	 * @return
	 */
	public static List<OrgTreeNode> convertToTreeNode(List<OmPositionForm> list){
		List<OrgTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(OmPositionForm position : list)
				result.add(convertToOrgTreeNode(position));
		}
		return result;
	}
	
	/**
	 * 将用户Form对象转换为OrgTreeNode对象
	 * @param form
	 * @return
	 */
	public static OrgTreeNode convertToOrgTreeNode(OmPositionForm form){
		if(form != null){
			OrgTreeNode node = new OrgTreeNode();
		//	node.setId(OrgTreeNode.getKey(OmPosition.class.getSimpleName(), form.getPosId()));
			node.setPartyType(OrgConstant.PARTY_TYPE_POSITION);
			node.setPosId(form.getPosId());
			node.setOrgId(form.getPartyId());
			node.setText(form.getPosName());
			node.setQtip(form.getPosCode());
			node.setIconCls(OrgTreeNode.CLS_POSITION);
			node.setLeaf(false);
			
			node.setCanManager(true);
			return node;
		}else
			return null;
	}
	
	public static List<OmPositionForm> convertToFormList(List<OmPosition> list){
		return convertToFormList(list,null);
	}
	
	public static List<OmPositionForm> convertToFormList(List<OmPosition> list, List<SysCodeForm> statusList){
		if(list != null && list.size() > 0){
			List<OmPositionForm> result = new ArrayList<OmPositionForm>();
			
			for(OmPosition entity : list){
				OmPositionForm form = convertToForm(entity);
				form.setStatusLabel(OrgUtils.getItemName(form.getStatus(), statusList));
				result.add(form);
			}
			
			return result;
		}else
			return null;
	}
	
	/**
	 * 将岗位Form对象的子岗位列表和用户列表转换为ExtTreeNode列表
	 * @param form
	 * @return
	 */
	public static List<OrgTreeNode> convertChildListToTreeNodeList(OmPositionForm form){
		List<OrgTreeNode> result = new ArrayList();
		
		if(form != null){
			
			//转换子机构列表
			if(form.getChildren() != null && form.getChildren().size() > 0){
				for(OmPositionForm pos : form.getChildren())
					result.add(convertToOrgTreeNode(pos));
			}
			
			
			//转换用户列表
			if(form.getUsers() != null && form.getUsers().size() > 0){
				for(OmUserForm user : form.getUsers())
					result.add(OmUserConverter.convertToOrgTreeNode(user, form.getPartyId(), form.getPosId()));
			}
		}
		
		return result;
	}
	
/*********************工作组树 GroupTree***************************/
	
	/**
	 * 将岗位Form对象的子岗位列表和用户列表转换为ExtTreeNode列表
	 * @param form
	 * @return
	 */
	public static List<GroupTreeNode> convertChildListToGroupTreeNodeList(OmPositionForm form){
		List<GroupTreeNode> result = new ArrayList();
		
		if(form != null){
			
			//转换子机构列表
			if(form.getChildren() != null && form.getChildren().size() > 0){
				for(OmPositionForm pos : form.getChildren())
					result.add(convertToGroupTreeNode(pos));
			}
			
			
			//转换用户列表
			if(form.getUsers() != null && form.getUsers().size() > 0){
				for(OmUserForm user : form.getUsers())
					result.add(OmUserConverter.convertToGroupTreeNode(user));
			}
		}
		
		return result;
	}
	
	
	/**
	 * 将用户Form对象转换为GroupTreeNode对象
	 * @param form
	 * @return
	 */
	public static GroupTreeNode convertToGroupTreeNode(OmPositionForm form){
		if(form != null){
			GroupTreeNode node = new GroupTreeNode();
			//node.setId(OrgTreeNode.getKey(OmPosition.class.getSimpleName(), form.getPosId()));
			node.setText(form.getPosName());
			node.setQtip(form.getPosCode());
			node.setIconCls(OrgTreeNode.CLS_POSITION);
			node.setLeaf(form.getIsLeaf() != null && form.getIsLeaf().equals(OrgConstant.ONE) ? true : false);
			return node;
		}else
			return null;
	}
	
	/**
	 * 将form对象列表转换为posId列表
	 * @param forms
	 * @return
	 */
	public static List<Long> convertFormsToPosIds(List<OmPositionForm> forms){
		List<Long> posIds = null;
		if(forms != null && forms.size()>0){
			posIds = new ArrayList<Long>();
			for(OmPositionForm form :forms)
				posIds.add(form.getPosId());
		}
		return posIds;
	}
}
